<?php
require_once("globals.php");

/* INSERT NEW USER TO LOGIN TABLE DATA BASE
 * this function adds a newly created user to the database.
 * return TRUE if succeeded, FALSE otherwise.
 */ 

function insert_new_userLogin_to_db($username, $password, $lastLoginTS, $active) {
	
	$db_info = mysql_query("INSERT INTO ".DB_TABLE_LOGIN." (username, password, loginLastTS, active) 
		    				VALUES ('". $username ."', UNHEX('". $password ."'), ". $lastLoginTS ." , ". $active .")");
	return $db_info;
}

function insert_new_userData_to_db($username, $firstname, $lastname, $sex, $age, $nationality) {
		
	$db_info = mysql_query("INSERT INTO ".DB_TABLE_USER_INFO." (username, firstname, lastname, sex, age, nationality) 
		    				VALUES ('". $username ."', '". $firstname ."', '". $lastname ."' , '". $sex ."' , '". $age ."' , '". $nationality ."')");
	return $db_info;
}













// -------------------------- SHADAP OLD FUCK ------------------------------
// Functions and Constants

// check_fields
// get_crypt_password
// check_if_logged
// is_checked
// check_login
// check_login_uncrypted
// find_highest_chick_id


// ---------------------------------------------------------------------------------------

function getMatchingGroups($q) {
	

	$db_info = mysql_query("SELECT group_name FROM ".DB_TABLE_GROUPS);
	
	$retVal = "";
	$i = 0;
	
	if (strlen($q)>0)
		while ($info = mysql_fetch_array($db_info, MYSQL_BOTH))
			{		
			if (strtolower($q) == strtolower(substr($info[$i],0,strlen($q))))
				$retVal = ($retVal == "") ? $info[$i] : $retVal . ", " . $info[$i];
			if ($i > 6)
				{
				$retVal = $retVal . "...";
				break;
				}
			}
	
	if ($retVal == "")
		return "-1";
	
	return $retVal;
	
}


//-----------------------------------------------------------------------------------------

function check_fields() {

if (($_POST['user']=='')||($_POST['pass']=='')||($_POST['pass2']=='')||
    ($_POST['name']=='')||($_POST['country']=='')||($_POST['sex']==''))
    return "1";
else if (!(is_numeric($_POST['age']))) return "2";
else if ($_POST['pass']!=$_POST['pass2']) return "3";
else return "0";
}

//-----------------------------------------------------------------------------------------

function get_crypt_password($username) {
	$db_info = mysql_query("SELECT * FROM ".DB_TABLE_LOGIN." WHERE user='".mysql_real_escape_string($username)."'");
	$info = mysql_fetch_array($db_info, MYSQL_BOTH);
	return $info['pass'];
}

//-----------------------------------------------------------------------------------------

function check_if_logged() {
  
  
  if (check_login($_SESSION['session_user'],$_SESSION['session_pass']))
	{
	return true;
	}
  else 
  	{
	return false;
	}
}

//-----------------------------------------------------------------------------------------

function is_checked($int) {
    if ($int=="1") return "CHECKED";
    else return "";
}

//-----------------------------------------------------------------------------------------

function check_login($username,$password) {
	$db_res = mysql_query("SELECT * FROM ".DB_TABLE_LOGIN." WHERE user='".mysql_real_escape_string($username)."' AND pass='".mysql_real_escape_string($password)."'");
	if (mysql_errno()) echo mysql_error();
	return (mysql_num_rows($db_res)==1);
}

//-----------------------------------------------------------------------------------------


function check_login_uncrypted($username,$password) {
	$db_res = mysql_query("SELECT * FROM ".DB_TABLE_LOGIN." WHERE user='".mysql_real_escape_string($username)."' AND pass=PASSWORD('".mysql_real_escape_string($password)."')");
	if (mysql_errno()) echo mysql_error();
	return (mysql_num_rows($db_res)==1);
}

//-----------------------------------------------------------------------------------------


function find_highest_chick_id() {

$highest_id=0;
$result = mysql_query("SELECT * FROM ".DB_TABLE_CHICKS);

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) 
    if ($highest_id<$row['chick_id'])
	$highest_id=$row['chick_id'];

return ($highest_id+1);
}



//-----------------------------------------------------------------------------------------
?>